from django.forms import model_to_dict
from rest_framework import serializers

from xiaochengxu import models


class RecursiveSerializer(serializers.Serializer):
    '''
    实现树状结构的类
    '''

    def to_representation(self, value):
        serializer = self.parent.parent.__class__(value, context=self.context)
        return serializer.data


class CreatSnsSerializers(serializers.ModelSerializer):

    class Meta:
        model = models.Sns_content
        exclude = ['user', ]




class GetSnsSerializers(serializers.ModelSerializer):
    # 第一中写法，关联别的表中的数据的第一种写法
    # avatar_url = serializers.CharField(source='user.avatar_url')
    # nickname = serializers.CharField(source='user.nickname')
    # gender = serializers.IntegerField(source='user.gender')

    # 第二中写法, get_user钩子函数中使用model_to_dict，吧user构造成一个字典
    user = serializers.SerializerMethodField()
    # 第三种写法，武佩奇教程中的，只是示例，在这个序列化器中不能用啊
    images = serializers.SerializerMethodField()

    class Meta:
        model = models.Sns_content
        fields = '__all__'

    # 第二种方法
    def get_user(self, obj):
        return model_to_dict(obj.user, fields=['id', 'avatar_url', 'nickname', 'gender'])

    # 第三种方法
    def get_images(self, obj):
        detail_querset = models.Sns_image.objects.filter(sns=obj)
        print(detail_querset)
        # 返回一个列表
        # return [row.path for row in detail_querset]
        # 返回一个字典
        # return [{'id': row.id, 'path':row.cos_path} for row in detail_queryset]
        return [model_to_dict(row, ['id', 'path']) for row in detail_querset]


class CreatCommentSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.Sns_comment
        exclude = ['user', ]


class GetCommentSerializers(serializers.ModelSerializer):
    avatar_url = serializers.CharField(source='user.avatar_url')
    nickname = serializers.CharField(source='user.nickname')
    gender = serializers.IntegerField(source='user.gender')
    user_id = serializers.CharField(source='user.uid')
    # parent = RecursiveSerializer(many=True, read_only=True)
    class Meta:
        model = models.Sns_comment
        fields = '__all__'


class GetSnsDetailSerializers(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()
    images = serializers.SerializerMethodField()
    # comment = serializers.SerializerMethodField()
    topic = serializers.SerializerMethodField()

    class Meta:
        model = models.Sns_content
        fields = '__all__'

    def get_user(self, obj):
        return model_to_dict(obj.user, fields=['id', 'avatar_url', 'nickname', 'gender'])

    def get_images(self, obj):
        detail_querset = models.Sns_image.objects.filter(sns=obj)
        return [model_to_dict(row, ['id', 'path']) for row in detail_querset]

    def get_topic(self, obj):
        if not obj.topic:
            return
        return model_to_dict(obj.topic, fields=['id', 'title'])

    # def get_comment(self, obj):
    #     comment_list = models.Sns_comment.objects.filter(sns_id=obj, depth=1).values(
    #         'id',
    #         'content',
    #         'user_id',
    #         'user__nickname',
    #         'user__avatar_url',
    #         'user__gender',
    #         'child_num',
    #         'creat_time',
    #     )
    #
    #     return comment_list

